package com.xiaoshuidi.cloud.module.finance.controller.admin.finance;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.finance.controller.admin.collection.vo.FinanceCollectionConfigRespVO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceDivideAccountDo;
import com.xiaoshuidi.cloud.module.finance.enums.finance.PayChannelTypeEnum;
import com.xiaoshuidi.cloud.module.finance.service.collection.FinanceCollectionConfigService;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceDivideService;
import com.xiaoshuidi.cloud.module.finance.service.finance.impl.FinanceDivideitory;
import com.xiaoshuidi.cloud.module.system.api.apartment.ApartmentManageApi;
import com.xiaoshuidi.cloud.module.system.enums.apartment.ApartmentManageEnum;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: lease-module-finance
 * @BelongsPackage: com.xiaoshuidi.cloud.module.finance.controller.admin.finance
 * @Author: James
 * @CreateTime: 2023-12-05  14:29
 * @Description: 分账设置
 * @Version: 1.0
 */
@Tag(name = "管理后台 - 分账设置")
@RestController
@RequestMapping("/finance/divideAccount")
public class FinanceDivideController {
    @Resource
    private FinanceDivideitory financeDivideService;
    @Resource
    private FinanceCollectionConfigService financeCollectionConfigService;

    @Resource
    private ApartmentManageApi apartmentManageApi;
    /*
     * @description:获取分账账户
     * @author: James
     * @date: 2023/12/5 17:47
     * @param: [apartmentId]
     * @return: com.xiaoshuidi.cloud.framework.common.pojo.CommonResult<java.util.List<com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceDivideAccountDo>>
     **/
    @GetMapping("/getdivideAccount")
    public CommonResult<List<FinanceDivideAccountDo>> getdivideAccount(Long apartmentId){
        Boolean isBigAccount=false;
        FinanceCollectionConfigRespVO financeConfig = financeCollectionConfigService.get(apartmentId, PayChannelTypeEnum.TongShangYun.getCode());
        FinanceDivideAccountDo financeDivideAccountDo=new FinanceDivideAccountDo();
        financeDivideAccountDo.setAccountNo(financeConfig.getCollectionNo());
        financeDivideAccountDo.setAccountName(financeConfig.getCollectionName());
        financeDivideAccountDo.setBigAccount(true);
        List<FinanceDivideAccountDo> financeDivideAccount = financeDivideService.getFinanceDivideAccount(apartmentId);
        if(financeDivideAccount.size()>0) {
            for (FinanceDivideAccountDo financeDivideAccountDo1 : financeDivideAccount) {
                if (financeDivideAccountDo1.getBigAccount()) {
                    isBigAccount=true;
                }
            }
        }
        if(isBigAccount){
            return CommonResult.success(financeDivideAccount);
        }else {
            financeDivideAccount.add(financeDivideAccountDo);
            return CommonResult.success(financeDivideAccount);
        }

    }
    /*
     * @description:新增分账
     * @author: James
     * @date: 2023/12/5 18:01
     * @param: [financeDivideAccountDos]
     * @return: com.xiaoshuidi.cloud.framework.common.pojo.CommonResult
     **/
    @PostMapping("/insertDivideAccount")
    public CommonResult insertDivideAccount(@RequestBody List<FinanceDivideAccountDo> financeDivideAccountDos){
        financeDivideService.insertFinanceDivide(financeDivideAccountDos);
        apartmentManageApi.updateApartment(financeDivideAccountDos.get(0).getApartmentId(), ApartmentManageEnum.SHAREPROFITS_CONFIG.getCode());
        return CommonResult.success();
    }
    /*
     * @description:
     * @author: James 修改分账账户信息
     * @date: 2023/12/5 18:20
     * @param: [financeDivideAccountDos]
     * @return: com.xiaoshuidi.cloud.framework.common.pojo.CommonResult
     **/
    @PostMapping("/updateDivideAccount")
    public CommonResult updateDivideAccount(@RequestBody List<FinanceDivideAccountDo> financeDivideAccountDos){
        financeDivideService.updateDivideAccount(financeDivideAccountDos);
        return CommonResult.success();
    }

    /*
     * @description:
     * @author: James 删除分账账户
     * @date: 2023/12/5 18:20
     * @param: [financeDivideAccountDos]
     * @return: com.xiaoshuidi.cloud.framework.common.pojo.CommonResult
     **/
    @GetMapping("/deleteDivideAccount")
    public CommonResult deleteDivideAccount(Long id){
        financeDivideService.removeById(id);
        return CommonResult.success();
    }
}
